package com.itheima.thread.threadpool.demo01;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class ThreadPool {

    private int poolSize = 2;

    private BlockingDeque<Runnable> blockingDeque = new LinkedBlockingDeque<Runnable>();

    public ThreadPool() {

        for (int i = 0; i < poolSize; i++) {
            new TaskThread("线程--->" + i).start();
        }
    }
    public void submit(Runnable runnable) {
        try {
            blockingDeque.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public class TaskThread extends Thread {

        public TaskThread(String name) {
            super(name);
        }

        @Override
        public void run() {


            while (true) {
                try {
                    Runnable runnable = blockingDeque.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
